/**
 * Created by Kay on 2016/3/8.
 */

function keyLogin(event) {

    var browser = navigator.appName;
    var userAgent = navigator.userAgent;
    var code;
    if(browser.indexOf('Internet')>-1) //IE
    code = window.event.keyCode;
    else if(userAgent.indexOf("Firefox")>-1)  //火狐
    code = event.which;
    else  //其它浏览器
    code = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;

    if ( code == 13)  //按Enter键的键值为13
        document.getElementById("btn_login").click();  //调用登录按钮的登录事件
}


window.onload = function () {
    var btn_login = document.getElementById('btn_login');
    var btn_register = document.getElementById('btn_register');


    btn_login.onclick = function login() {
        //进行表单验证
        if($("[name='loginname']").val().length==0){
            $("#msglogin").html("账号不能为空");
            return;
        }
        if($("[name='password']").val().length==0){
            $("#msgpwd").html("密码不能为空");
            return;
        }
        $.ajax({
            url:"http://localhost:8080/getUserSome",
            type:"GET",
            data:$("#loginForm1").serialize(),//serialize()能将表单封装进行传递
            dataType:"json",
            success:function(rs){
                if (rs.ok){
                    //登陆成功
                    //1.在本地记录登录用户的id
                    $.each(rs.data.list,function (i,n) {
                        sessionStorage.setItem("uname",n.username);
                        //console.info(sessionStorage.getItem("uid"))
                    })
                    //2.跳转后台页面
                    window.location.href="Index.html";
                } else{
                    alert(rs.respMsg);
                }
            },
            error:function (rs) {
            }
        });

    }

    btn_register.onclick = function register() {
        window.open("register.html");
    }
}


